import Vue from 'vue'
import CryptoJS from 'crypto-js'
// 默认的 KEY 与 IV
const KEY = CryptoJS.enc.Utf8.parse("ninemax");
const IV = CryptoJS.enc.Utf8.parse("ninemax");
const RESTORE_KEY = "t";

export default {//加密
  encrypt(word, keyStr, ivStr) {
    let key = KEY;
    let iv = IV;

    if (keyStr && ivStr) {
      console.log('----自定义---key,iv')
      key = CryptoJS.enc.Utf8.parse(keyStr);
      iv = CryptoJS.enc.Utf8.parse(ivStr);
    }
    let srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.ZeroPadding
    });
    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
  },

  //解密
  decrypt(word, keyStr, ivStr) {
    let key = KEY;
    let iv = IV;

    if (keyStr && ivStr) {
      key = CryptoJS.enc.Utf8.parse(keyStr);
      iv = CryptoJS.enc.Utf8.parse(ivStr);
    }
    let base64 = CryptoJS.enc.Base64.parse(word);
    let src = CryptoJS.enc.Base64.stringify(base64);
    var decrypt = CryptoJS.AES.decrypt(src, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.ZeroPadding
    });
    var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
  },

  //异或运算加密
  Restore(str, keyIndexStr) {
    let keyIndex = RESTORE_KEY;
    if(keyIndexStr){
      keyIndex = keyIndexStr;
    }
    var resultStr = "";
    var arr = [...str];
    for (var i = 0; i < arr.length; i++) {
      resultStr += String.fromCharCode(arr[i].charCodeAt() ^ keyIndex);//keyIndex是与后端约定好的，得到加密字符的unicode编码与keyIndex进行异或，最后得到异或过的字符
    }
    return resultStr;
  },
}
